package com.edu.policy.controller;

import com.edu.policy.common.api.CommonResult;
import com.edu.policy.domin.PageDto;
import com.edu.policy.model.DmsDevices;
import com.edu.policy.model.DmsFunctionCodes;
import com.edu.policy.model.UmsMenu;
import com.edu.policy.service.AppDmsDevicesService;
import com.edu.policy.service.DmsDevicesService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RequiredArgsConstructor
@Controller
@Api(tags = "DmsDeviceController")
@RequestMapping("/device")
public class DmsDeviceController {
    private final AppDmsDevicesService devicesService;
    @ApiOperation("添加设备信息")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> create(@RequestBody DmsDevices dmsDevices) {
        dmsDevices.setCreatedAt(new Date());
        boolean count = devicesService.save(dmsDevices);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("修改设备信息")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> update(@PathVariable Integer id,
                                                 @RequestBody DmsDevices dmsDevices) {
        dmsDevices.setId(id);
        boolean count = devicesService.updateById(dmsDevices);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("根据ID获取菜单详情")
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<DmsDevices> getItem(@PathVariable Integer id) {
        DmsDevices dmsDevices = devicesService.getById(id);
        return CommonResult.success(dmsDevices);
    }

    @ApiOperation("根据ID删除设备信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    @ResponseBody
    public CommonResult<ObjectUtils.Null> delete(@PathVariable Integer id) {
        boolean count = devicesService.removeById(id);
        if (count) {
            return CommonResult.success(null);
        } else {
            return CommonResult.failed();
        }
    }

    @ApiOperation("分页查询设备信息")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public CommonResult<PageDto<DmsDevices>> list(@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
                                               @RequestParam(value = "currentPage", defaultValue = "1") Integer currentPage) {
        PageDto<DmsDevices> devicesList = devicesService.list(pageSize, currentPage);
        return CommonResult.success(devicesList);
    }
}
